package com.cyan.teamquestionnaire.service.Impl;

import com.cyan.teamquestionnaire.beans.PageInfo;
import com.cyan.teamquestionnaire.entity.AnswerSheet;
import com.cyan.teamquestionnaire.entity.Questionnaire;
import com.cyan.teamquestionnaire.entity.User;
import com.cyan.teamquestionnaire.mappers.AnswerSheetMapper;
import com.cyan.teamquestionnaire.mappers.QuestionnaireMapper;
import com.cyan.teamquestionnaire.service.AnswerSheetService;
import com.cyan.teamquestionnaire.service.GroupService;
import com.cyan.teamquestionnaire.service.QuestionnaireService;
import com.cyan.teamquestionnaire.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnswerSheetServiceImpl implements AnswerSheetService {

    @Resource
    QuestionnaireMapper questionnaireMapper;

    @Resource
    AnswerSheetMapper answerSheetMapper;

    @Resource
    QuestionnaireService questionnaireService;

    @Resource
    private GroupService groupService;

    // 查询name需要（已经）回答的问卷
    @Override
    public List<Questionnaire> questionnaireForOne(String name, boolean need) {
        List<String> qidL = null;
        List<Questionnaire> list = new ArrayList<>();
        // 找到对应的qid集合
        if (need) qidL = answerSheetMapper.needToAnswer(name);
        else qidL = answerSheetMapper.haveAnswered(name);

        for (String qid : qidL) {
            Questionnaire questionnaire = questionnaireMapper.queryQuestionnaireByQidEq(qid);
            if (questionnaire != null) {
                list.add(questionnaire);
            }
        }
        return list;
    }

    // 插入答卷内容
    @Override
    public void insertAnswer(List<AnswerSheet> list) {
        for (AnswerSheet answerSheet : list) {
            answerSheetMapper.insertAnswer(answerSheet.getName(), answerSheet.getQId());
        }
    }

    // 查询name需要（已经）回答的问卷——————分页
    @Override
    public PageInfo<Questionnaire> questionnaireForOneY(PageInfo<Questionnaire> pageInfo, boolean need) {

        List<String> qidL = null;
        List<Questionnaire> list = new ArrayList<>();
        // 找到对应的qid集合
        if (need) qidL = answerSheetMapper.needToAnswer(pageInfo.getName());
        else qidL = answerSheetMapper.haveAnswered(pageInfo.getName());
        for (String qid : qidL) {
           Questionnaire questionnaire= questionnaireMapper.queryQuestionnaireByQidTitle(qid, "%" + pageInfo.getQuery() + "%");
            if (questionnaire!=null){
                list.add(questionnaire);
            }

        }
        return questionnaireService.splitPage(pageInfo, list);

    }

    //name需要（已经）回答的问卷,通过Qid查询问卷以及title进行模糊判断
    @Override
    public List<Questionnaire> questionnaireForOneFuzzy(String name, String title, boolean need) {
        List<String> qidL = null;
        List<Questionnaire> list = new ArrayList<>();
        // 找到对应的qid集合
        if (need) qidL = answerSheetMapper.needToAnswer(name);
        else qidL = answerSheetMapper.haveAnswered(name);
        for (String qid : qidL) {
            Questionnaire questionnaire = questionnaireMapper.queryQuestionnaireByQidTitle(qid, "%" + title + "%");
            if (questionnaire != null) list.add(questionnaire);
        }
        return list;
    }

    //回答问题
    @Override
    public void answerQuestionnaire(AnswerSheet answerSheet) {
        answerSheet.setAnsTime(DateUtils.currentDate());
        answerSheetMapper.answerQuestionnaire(answerSheet);
    }

    // qid需要（已经）回答的问卷, need==true表示该问卷还没有被回答
    @Override
    public List<AnswerSheet> questionnaireForSelf(String qId,String name, boolean need) {
        List<AnswerSheet> answerSheetList;
        // 找到对应的问卷集合
        if (need) answerSheetList = answerSheetMapper.questionnaireNotAnswered(qId, "%" + name + "%");
        else answerSheetList = answerSheetMapper.questionnaireAnswered(qId, "%" + name + "%");
        return answerSheetList;
    }

    //qid需要（已经）回答的问卷, need==true表示该问卷还没有被回答————分页
    //传入qid,对name进行模糊查询
    public PageInfo<AnswerSheet> questionnaireForSelfY(PageInfo<AnswerSheet> pageInfo, boolean need) {
            List<AnswerSheet> list = questionnaireForSelf(pageInfo.getQId(),"%"+pageInfo.getQuery()+"%",need);
            return (PageInfo<AnswerSheet>) (PageInfo) groupService.splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list);
    }

    //根据QID和name查询问卷回答情况
    @Override
   public AnswerSheet AnswerbyQidName(AnswerSheet answerSheet){
        return answerSheetMapper.AnswerbyQidName(answerSheet.getQId(),answerSheet.getName());
    }

    //用户发布的所有的问卷的答卷率，传入用户的name，
    @Override
    public Map<String,Object>  userResponseRate(String name){
        //得到List
        List<Questionnaire> questionnaireList = questionnaireMapper.selectByCreatorName(name);
        int Answered = 0;//已经回答的
        int NotAnswered = 0;//没有回答的

        for ( Questionnaire questionnaire : questionnaireList) {
            NotAnswered   += answerSheetMapper.questionnaireNotAnswered(questionnaire.getQId(),"%%").size();
            Answered  += answerSheetMapper.questionnaireAnswered(questionnaire.getQId(), "%%").size();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("Answered",Answered);
        map.put("NotAnswered",NotAnswered);
        if (Answered+NotAnswered !=0){
            double rate = (double)Answered/ (double) (Answered+NotAnswered);
            map.put("rate",rate);
        }
        return map;
    }


}
